package com.ffyc.cemp.common.handler;

import com.auth0.jwt.exceptions.TokenExpiredException;
import com.ffyc.cemp.common.dto.HttpResult;
import com.ffyc.cemp.common.exception.user.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;


/**
 * 项目全局异常处理
 */
@Slf4j
@RestControllerAdvice
public class CempExceptionHandler {

    @ExceptionHandler(UsernameHasExistedException.class)
    public HttpResult<String> handlerUsernameHasExistedException(UsernameHasExistedException e) {
        log.debug("注册异常:{}",e.getMessage());
        return HttpResult.failed(e.getMessage());
    }

    @ExceptionHandler(AccountNotFoundException.class)
    public HttpResult<String> handlerAccountNotFoundException(AccountNotFoundException e) {
        log.debug("用户登录异常:{}",e.getMessage());
        return HttpResult.failed(e.getMessage());
    }

    @ExceptionHandler(BadCredentialsException.class)
    public HttpResult<String> handBadCredentialsException(BadCredentialsException e) {
        log.debug("用户登录异常:{}",e.getMessage());
        return HttpResult.failed(e.getMessage());
    }

    @ExceptionHandler(NoTokenException.class)
    public HttpResult<String> handNoTokenException(NoTokenException e) {
        log.debug("前端token未携带异常:{}",e.getMessage());
        return HttpResult.failed(e.getMessage());
    }

    @ExceptionHandler(TokenNotFoundException.class)
    public HttpResult<String> handTokenNotFoundException(TokenNotFoundException e) {
        log.debug("token存在异常:{}",e.getMessage());
        return HttpResult.failed(e.getMessage());
    }

    @ExceptionHandler(TokenExpiredException.class)
    public HttpResult<String> handTokenExpiredDateException(TokenExpiredException e) {
        log.debug("token过期异常:{}",e.getMessage());
        return HttpResult.failed("token已过期 ");
    }

    @ExceptionHandler(PermissionDeniedException.class)
    public HttpResult<String> handPermissionDeniedException(PermissionDeniedException e){
        log.debug("用户访问权限异常:{}",e.getMessage());
        return HttpResult.failed(e.getMessage());
    }

    @ExceptionHandler(VCodeExpiredException.class)
    public HttpResult<String> handVCodeExpiredException(VCodeExpiredException e){
        return HttpResult.failed(e.getMessage());
    }

    @ExceptionHandler(VCodeErrorException.class)
    public HttpResult<String> handVCodeErrorException(VCodeErrorException e){
        return HttpResult.failed(e.getMessage());
    }
}

